package cn.wen.base.image.glide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import cn.wen.base.R;
import cn.wen.base.image.IImageEngine;


/**
 * Created by Administrator on 2017/6/19.
 */

public class GlideEngine implements IImageEngine {

    public static final float SIZE_MULTIPLIER = 0.1f;// 模糊加载
    public static final int DURATION = 200;//动画时长
    private RequestOptions mOptions = new RequestOptions()
            .fitCenter()
            .override(800, 480)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
    private RequestOptions mOptions2 = new RequestOptions()
            .fitCenter()
            .override(800, 480)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE);

    @Override
    public void displayImage(Context context, ImageView imageView, String imageUrl, int errorResId) {
        mOptions.placeholder(errorResId).error(errorResId);
        displayImage(context, imageView, imageUrl, mOptions);
    }

    @Override
    public void displayImage(Context context, ImageView imageView, String imageUrl) {
        mOptions.placeholder(R.drawable.transparent).error(R.drawable.transparent);
        displayImage(context, imageView, imageUrl, false);
    }

    @Override
    public void displayImage(Context context, ImageView imageView, String imageUrl, boolean isAnimation) {
        if (isAnimation) {
            Glide.with(context)
                    .load(imageUrl)
                    .apply(mOptions2)
                    .transition(DrawableTransitionOptions.withCrossFade(DURATION))
                    .thumbnail(SIZE_MULTIPLIER)
                    .into(imageView);
        } else {
            Glide.with(context)
                    .load(imageUrl)
                    .apply(mOptions2)
                    .thumbnail(SIZE_MULTIPLIER)
                    .into(imageView);
        }

    }

    private void displayImage(Context context, ImageView imageView, String imageUrl, RequestOptions options) {
        Glide.with(context)
                .load(imageUrl)
                .apply(options)
                .thumbnail(SIZE_MULTIPLIER)
                .into(imageView);
    }


    @Override
    public void displayImage(Context context, ImageView imageView, int resId) {
        Glide.with(context)
                .asFile()
                .thumbnail(SIZE_MULTIPLIER)
                .load(resId)
                .apply(mOptions)
                .into(imageView);
    }

    @Override
    public void displayCircleImage(Context context, ImageView imageView, String imageUrl, int errorResId) {
        mOptions.placeholder(errorResId).error(errorResId);
        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .thumbnail(SIZE_MULTIPLIER)
                .apply(mOptions)
                .into(new CircularViewTarget(context, imageView));
    }

    @Override
    public void displayRadiusImage(Context context, ImageView imageView, String imageUrl, int errorResId, float radius) {
        mOptions.placeholder(errorResId).error(errorResId);
        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .thumbnail(SIZE_MULTIPLIER)
                .apply(mOptions)
                .into(new RadiusViewTarget(context, imageView, radius));
    }

    /**
     * 加载图片显示至背景
     */
    @Override
    public void displayView(Context context, final View view, String imageUrl, int errorResId) {
        mOptions.placeholder(errorResId).error(errorResId);
        Glide.with(context)
                .asDrawable()
                .load(imageUrl)
                .apply(mOptions)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        if (resource == null || view == null) {
                            return;
                        }
                        view.setBackgroundDrawable(resource);
                    }
                });
    }

    @Override
    public void clear(Context context) {
        Glide.get(context).clearMemory();
    }


}
